import { YhBaseEntity, YhEntity } from "@/common/baseentity";
import { Column, Entity, JoinColumn, ManyToOne, OneToMany, PrimaryColumn } from "typeorm";
import { decimalTransformer } from "@/common/transform/DecimalTransformer";
import { FeeDet } from "@/business/entities/business.entity";


@Entity({
    name: 'contract'
})
export class Contract extends YhBaseEntity {
    @Column({
        type: 'int',
        name: 'client_cod',
        comment: '客户'
    })
    client_cod: number;

    @Column({
        type: 'varchar',
        name: 'shipper',
        comment: '发货人'
    })
    shipper: string;

    @Column({
        type: 'varchar',
        name: 'consignee',
        comment: '收货人'
    })
    consignee: string;

    @Column({
        type: 'varchar',
        name: 'ship_nam',
        comment: '船名'
    })
    ship_nam: string;

    @Column({
        type: 'varchar',
        name: 'voyage',
        comment: '航次'
    })
    voyage: string;

    @Column({
        type: 'varchar',
        name: 'bill_no',
        comment: '提单号'
    })
    bill_no: string;

    @Column({
        type: 'varchar',
        name: 'depart_port',
        comment: '启运港'
    })
    depart_port: string;

    @Column({
        type: 'varchar',
        name: 'dest_port',
        comment: '目的港'
    })
    dest_port: string;

    @Column({
        type: 'decimal',
        precision: 10,
        scale: 2,
        name: 'cargo_netwgt',
        comment: '净重',
        transformer: decimalTransformer
    })
    cargo_netwgt: number;

    @Column({
        type: 'decimal',
        precision: 10,
        scale: 2,
        name: 'cargo_scalewgt',
        comment: '毛重',
        transformer: decimalTransformer
    })
    cargo_scalewgt: number;

    @Column({
        type: 'decimal',
        precision: 11,
        scale: 3,
        name: 'cargo_volume',
        comment: '体积',
        transformer: decimalTransformer
    })
    cargo_volume: number;

    @Column({
        type: 'int',
        name: 'cargo_quantity',
        comment: '件数'
    })
    cargo_quantity: number;

    @Column({
        type: 'varchar',
        name: 'trans_kind',
        comment: '运输方式'
    })
    trans_kind: string;

    @Column({
        type: 'varchar',
        name: 'pay_kind',
        comment: '付款方式'
    })
    pay_kind: string;
    
    @OneToMany(() => ContractCntrSum, (cs) => cs.contract)
    cntrsum: ContractCntrSum[];

    @OneToMany(() => ContractTrans, (ct) => ct.contract)
    trans: ContractTrans[];

    @OneToMany(() => FeeDet, (c) => c.contract)
    feedets: FeeDet[];
}

@Entity({
    name: 'contract_cntr_sum'
})
export class ContractCntrSum extends YhBaseEntity {
    @Column({
        type: 'int',
        name: 'contract_id',
        comment: '委托ID'
    })
    contract_id: number;

    @Column({
        type: 'varchar',
        name: 'cntr_siz_cod',
        comment: '尺寸'
    })
    cntr_siz_cod: string;

    @Column({
        type: 'varchar',
        name: 'cntr_typ_cod',
        comment: '箱型'
    })
    cntr_typ_cod: string;

    @Column({
        type: 'int',
        name: 'cntr_num',
        comment: '箱量'
    })
    cntr_num: number;

    @ManyToOne(() => Contract, (c) => c.cntrsum)
    @JoinColumn({
        name: 'contract_id'
    })
    contract: Contract;

    @OneToMany(() => ContractCntr, (c) => c.cntrsum)
    cntrs: ContractCntr[];
}

@Entity({
    name: 'contract_cntr'
})
export class ContractCntr extends YhBaseEntity {
    @Column({
        type: 'int',
        name: 'contract_cntr_sum_id',
        comment: 'contract_cntr_sum.id'
    })
    contract_cntr_sum_id: number;

    @Column({
        type: 'varchar',
        name: 'cntr',
        comment: '箱号'
    })
    cntr: string;

    @ManyToOne(() => ContractCntrSum, (cs) => cs.cntrs)
    @JoinColumn({
        name: 'contract_cntr_sum_id'
    })
    cntrsum: ContractCntrSum;

}

@Entity({
    name: 'contract_trans'
})
export class ContractTrans extends YhBaseEntity {
    @Column({
        type: 'int',
        name: 'contract_id',
        comment: 'contract.id'
    })
    contract_id: number;

    @Column({
        type: 'int',
        name: 'seq',
        comment: '序号'
    })
    seq: number;

    @Column({
        type: 'varchar',
        name: 'depart_place',
        comment: '起运地',
    })
    depart_place: string;

    @Column({
        type: 'varchar',
        name: 'dest_place',
        comment: '目的地'
    })
    dest_place: string;

    @ManyToOne(() => Contract, (c) => c.trans)
    @JoinColumn({
        name: 'contract_id'
    })
    contract: Contract;
}

